package wechat.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import wechat.dao.AccessTokenDao;
import wechat.entity.AccessTokenEntity;
import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.service.AccessTokenService;
import wechat.service.base.BaseRequest;
import wechat.util.date.DateUtil;

@Service
public class IAccessTokenService extends BaseRequest implements AccessTokenService {

	private Logger log = LoggerFactory.getLogger(IAccessTokenService.class);

	@Autowired
	private AccessTokenDao accessTokenDao;

	public AccessTokenEntity getAccessToken(String appid) throws Exception {
		log.info("IAccessTokenService--getAccessToken appid=" + appid);

		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		AccessTokenEntity newAccessToken = accessToken(instanceResource);
		
		return newAccessToken;
	}


	public AccessTokenEntity getAccessToken(InstanceResource instanceResource) throws Exception {
		AccessTokenEntity newAccessToken = accessToken(instanceResource);
		return newAccessToken;
	}
	
	public void resetAccessToken(String appid) throws Exception {
		InstanceResource instanceResource = AppFilter.chooseApp(appid);
		
		long currentTime = DateUtil.getCurrentTime();
		AccessTokenEntity newAccessToken = new AccessTokenEntity();
		
		String accessToken = super.fetchAcessToken(instanceResource);
		newAccessToken.setAccessToken(accessToken);
		newAccessToken.setModifyTime(currentTime);

		accessTokenDao.updateAccessToken(newAccessToken);

	}
	
	
	private AccessTokenEntity accessToken(InstanceResource instanceResource) throws Exception {
//		String appid = instanceResource.getAppid();
		
		long currentTime = DateUtil.getCurrentTime();
		
		AccessTokenEntity newAccessToken = new AccessTokenEntity();
		String accessToken = null;
		
		AccessTokenEntity accessTokenEntity = accessTokenDao.getAccessToken();
		if (accessTokenEntity == null) {
			accessToken = super.fetchAcessToken(instanceResource);
			
			newAccessToken.setAccessToken(accessToken);
			newAccessToken.setCreateTime(currentTime);
			newAccessToken.setModifyTime(currentTime);
			accessTokenDao.addAccessToken(newAccessToken);

			log.info("IAccessTokenService--accessToken Get New AccessToken accessToken=" + accessToken);

		
		}
		else {
			long modifyTime = accessTokenEntity.getModifyTime();
//			System.out.println(DateUtil.getDateFormat(new Date(currentTime), DateUtil.simple));
//			System.out.println(DateUtil.getDateFormat(new Date(modifyTime),DateUtil.simple));
			boolean timeOut = (currentTime - modifyTime) > super.Deadline * 1000;
			
			newAccessToken = accessTokenEntity;
			
			if (timeOut) {
				accessToken = super.fetchAcessToken(instanceResource);
				newAccessToken.setAccessToken(accessToken);
				newAccessToken.setModifyTime(currentTime);

				accessTokenDao.updateAccessToken(newAccessToken);

				log.info("IAccessTokenService--accessToken Get New AccessToken accessToken=" + accessToken);
			}
		}
		
		log.info("IAccessTokenService--accessToken Get AccessToken newAccessToken=" + newAccessToken);
		return newAccessToken;
	}




}
